/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.woundedwolf.android.tumbleweed.examples;

import android.util.Log;
import com.woundedwolf.android.tumbleweed.AbstractGame;
import com.woundedwolf.android.tumbleweed.Engine;
import com.woundedwolf.android.tumbleweed.Particle;
import com.woundedwolf.android.tumbleweed.Pixel;
import com.woundedwolf.android.tumbleweed.renderables.Pixels;
import com.woundedwolf.android.tumbleweed.elements.ActionVector;
import com.woundedwolf.android.tumbleweed.elements.Vector2D;
import java.util.Random;

/**
 *
 * @author FlameDuck
 */
public class Fireworks extends AbstractGame
{
	private Pixels pixels;
	private Random random;
	private String stat;

	public Fireworks(Engine engine)
	{
		super(engine);
		random = new Random(System.currentTimeMillis());
		pixels = new Pixels(512);
		addRenderable(pixels);
		stat = pixels.getPixelStats();
	}

	@Override
	public void updateGame()
	{
		if (!pixels.getPixelStats().equals(stat))
		{
			Log.d("Tumbleweed4Android", stat);
		}
		stat = pixels.getPixelStats();

		ActionVector blastPoint = dequeueAction();
		if (blastPoint != null)
		{
			for(int i=0; i<32; i++)
			{
				Particle p = new Particle(new Vector2D(blastPoint.getXpos(), blastPoint.getYpos()), 25);
				Vector2D speed = new Vector2D(random.nextFloat()*360);
				speed.scale(random.nextFloat()*4);
				p.moveParticle(speed, true);
				pixels.addPixel(p);
			}
		}

		for (Pixel pixel : pixels.getPixels())
		{
			if (pixel instanceof Particle)
			{
				((Particle)pixel).update();
				pixels.updatePixel(pixel);
				if (((Particle)pixel).isDead())
					pixels.removePixel(pixel);
			}
		}
	}
}
